package com.loginlib.presenters;

import android.text.TextUtils;
import android.util.Log;

import com.loginlib.login.LoginActivity;
import com.loginlib.panel.ILoginView;
import com.wanhe.base.model.entity.LoginEntity;
import com.wanhe.base.model.entity.VersionEntity;
import com.wanhe.base.net.ServerApi;
import com.wanhe.base.presents.BasePresenter;
import com.wanhe.base.runner.Runner;
import com.wanhe.base.util.Logger;
import com.wanhe.base.util.SPUtils;
import com.wanhe.base.websocket.WebSocketTool;
import com.wanhe.base.websocket.callback.LoginCallBack;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import java.net.URISyntaxException;

import javax.inject.Inject;
import javax.inject.Singleton;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2016/7/18.
 */
@Singleton
public class UserCenterPresenter extends BasePresenter {

    ILoginView loadingView;

    public void setLoadingView(ILoginView loadingView) {
        this.loadingView = loadingView;
    }

    @Inject
    public UserCenterPresenter(ServerApi api, Runner runner) {
        super(api, runner);
    }


    public void checkLogin(final String ip, final String port, final String OperID, final String Password) {
        if (loadingView != null) {
            loadingView.onLoading();
        }
        runner.execute(api.checkLogin(ip, port, OperID, Password), new Consumer<LoginEntity>() {

            @Override
            public void accept(@NonNull LoginEntity entity) throws Exception {
                Logger.d("onNext");
                if (entity != null) {
                    if (entity.resultType == LoginEntity.SUCCESS) {
                        if (loadingView != null) {
                            loadingView.checkLoginSuccess(ip, port);
                        }
                    } else if (entity.resultType == LoginEntity.FAILE) {
                        if (entity.resultMsg != null) {
                            loadingView.onLoadFail(entity.resultMsg);
                        }
                    }
                }
            }
        });
    }

    public void login(final String OperID, final String Password) {
        if (loadingView != null) {
            loadingView.onLoading();
        }
        runner.execute(api.login(OperID, Password),

                new Consumer<LoginEntity>() {

                    @Override
                    public void accept(@NonNull LoginEntity entity) throws Exception {
                        Logger.d("onNext");
                        if (entity != null) {
                            if (entity.resultType == LoginEntity.SUCCESS) {
                                if (loadingView != null) {
                                    loadingView.loginSuccess(OperID, Password);
                                }
                            } else if (entity.resultType == LoginEntity.FAILE) {
                                if (entity.resultMsg != null) {
                                    loadingView.onLoadFail(entity.resultMsg);
                                }
                            }
                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void initWebSocket(){
        try {
            WebSocketTool.getInstance().initWebSockClient();
            WebSocketTool.getInstance().connect();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public void setInitWebsocket(WebSocketTool.WebSocketInitCallBack initCallBack){
        WebSocketTool.getInstance().setInitCallBack(initCallBack);
    }

    public void login2(String user, String pws, String machineCode, LoginCallBack callBack){
        int tag =(int)System.currentTimeMillis();
        WebSocketTool.getInstance().padLogin(tag,user,pws,machineCode,callBack);
    }

    public void getVersion(){
        runner.execute(api.getVersion(), new Consumer<VersionEntity>() {
            @Override
            public void accept(@NonNull VersionEntity entity) throws Exception {
                Log.i("initData","entity="+entity.Version);
                Log.i("initData","resultType="+entity.resultType);
                Log.d("initData","(SPUtils.getVersion()="+SPUtils.getVersion());
                if(entity.resultType==VersionEntity.SUCCESS){
                    if(!TextUtils.isEmpty(entity.Version)&&!entity.Version.equals(SPUtils.getVersion())) {
                        if(loadingView!=null){
                            loadingView.updateVersion(entity.Version);
                        }
                    }
                }
            }
        });
    }

    public void heartBeat(){
        runner.execute(WebSocketTool.getInstance().sendHeartbeat(), new Subscriber() {
            @Override
            public void onSubscribe(Subscription s) {
                Log.d("userCenter","onSubscribe");
            }

            @Override
            public void onNext(Object o) {
                Log.d("userCenter","onNext");
            }

            @Override
            public void onError(Throwable t) {
                Log.d("userCenter","onError t="+t);
            }

            @Override
            public void onComplete() {
                Log.d("userCenter","onComplete");
            }
        });
    }
}
